using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;


namespace MM.Business
{
    public class clsCHI_TIET_PXH
    {
        public clsCHI_TIET_PXH(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        #region Private methods
        private CHI_TIET_PXH GetDataRow(DataRow row)
        {
            CHI_TIET_PXH objCHI_TIET_PXH = new CHI_TIET_PXH();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PXH.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }            
            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objCHI_TIET_PXH.MaKho = (row["MaKho"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuHuyXuatHang"] != DBNull.Value)
                    objCHI_TIET_PXH.MaPhieuHuyXuatHang = (row["MaPhieuHuyXuatHang"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuXuatHang"] != DBNull.Value)
                    objCHI_TIET_PXH.MaPhieuXuatHang = (row["MaPhieuXuatHang"].ToString());
            }
            catch { }
          
          
            try
            {
                if (row["TinhTrang"] != DBNull.Value)
                    objCHI_TIET_PXH.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            }
            catch { }

            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    objCHI_TIET_PXH.SoLuong = float.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    objCHI_TIET_PXH.MaThietBi = (row["MaThietBi"].ToString());
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objCHI_TIET_PXH.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PXH.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PXH.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["POThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PXH.POThamChieuSAP = (row["POThamChieuSAP"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKhoThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PXH.MaKhoThamChieuSAP = (row["MaKhoThamChieuSAP"].ToString());
            }
            catch { }
            try
            {
                if (row["ValuationType"] != DBNull.Value)
                    objCHI_TIET_PXH.ValuationType = (row["ValuationType"].ToString());
            }
            catch { }
            try
            {
                if (row["MaChiTietPDH"] != DBNull.Value)
                    objCHI_TIET_PXH.MaChiTietPDH = int.Parse(row["MaChiTietPDH"].ToString());
            }
            catch { }
            try
            {
                if (row["NgayHuyXuatHang"] != DBNull.Value)
                    objCHI_TIET_PXH.NgayHuyXuatHang = Convert.ToDateTime(row["NgayHuyXuatHang"].ToString());
            }
            catch { }
            try
            {
                if (row["MaNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PXH.MaNhaSanXuat = (row["MaNhaSanXuat"].ToString());
            }
            catch { }

            return objCHI_TIET_PXH;
        }
        private List<CHI_TIET_PXH> GetCHI_TIET_PXH(DataTable dt)
        {
            List<CHI_TIET_PXH> lst = new List<CHI_TIET_PXH>();
            CHI_TIET_PXH obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods
        public List<CHI_TIET_PXH> usp_CheckSoLuongXuatKhoChuaDuyet(string MaVTPT, string MaKho,int MaTrangThai)
        {
            string spName = "usp_CheckSoLuongXuatKhoChuaDuyet";
            DataTable dt = new DataTable();
            List<CHI_TIET_PXH> lst = new List<CHI_TIET_PXH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[3];

               
                par[0] = new SqlParameter("@MaVTPT", MaVTPT);
                par[1] = new SqlParameter("@MaKho", MaKho);
                par[2] = new SqlParameter("@MaTrangThai", MaTrangThai);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PXH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public List<CHI_TIET_PXH> usp_SelectCHI_TIET_PXH_ByMaPhieuXuatHang(string MaPhieuXuatHang)
        {
            string spName = "usp_SelectCHI_TIET_PXH_ByMaPhieuXuatHang";
            DataTable dt = new DataTable();
            List<CHI_TIET_PXH> lst = new List<CHI_TIET_PXH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaPhieuXuatHang", MaPhieuXuatHang);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PXH(dt);
            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public List<CHI_TIET_PXH> usp_SelectCHI_TIET_PXH_ByPOThamChieuSAP(string POThamChieuSAP)
        {
            string spName = "usp_SelectCHI_TIET_PXH_ByPOThamChieuSAP";
            DataTable dt = new DataTable();
            List<CHI_TIET_PXH> lst = new List<CHI_TIET_PXH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@POThamChieuSAP", POThamChieuSAP);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PXH(dt);
            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public int usp_UpdateCHI_TIET_PXH_Cancelation(List<CHI_TIET_PXH> lstCT)
        {
            string spName = "usp_UpdateCHI_TIET_PXH_Cancelation";
            int kq = 0;
            try
            {
                CHI_TIET_PXH obj;
                SqlHelper.BeginTran();
                int i = 0;
                for (i = 0; i < lstCT.Count; i++)
                {
                    obj = new CHI_TIET_PXH();
                    obj = lstCT[i];

                    SqlParameter[] par = new SqlParameter[9];
                    par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                    par[0].Direction = ParameterDirection.ReturnValue;
                    par[1] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                    par[2] = new SqlParameter("@SoLuong", obj.SoLuong);                    
                    par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                    par[4] = new SqlParameter("@MaPhieuXuatHang", obj.MaPhieuXuatHang);
                    par[5] = new SqlParameter("@MaPhieuHuyXuatHang", obj.MaPhieuHuyXuatHang);
                    par[6] = new SqlParameter("@MaKho", obj.MaKho);
                    par[7] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                    par[8] = new SqlParameter("@NgayTao", obj.NgayNhap);

                    if (!SqlHelper.IsHaveTran())
                        SqlHelper.ExecuteNonQuery(spName, par);
                    else
                    {
                        SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                    }
                    kq = int.Parse(par[0].Value.ToString());
                    if (kq <= 0)
                        SqlHelper.Tran.Rollback();
                }

                if (i == lstCT.Count)
                    SqlHelper.Tran.Commit();
                else
                    SqlHelper.Tran.Rollback();

            }
            catch 
            {
                kq = -1;
                //SqlHelper.Tran.Rollback();
            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_UpdateCHI_TIET_PXH(CHI_TIET_PXH obj)
        {
            string spName = "usp_UpdateCHI_TIET_PXH";
            int kq = 0;
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[9];

                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@MaThietBi", obj.MaThietBi);
                par[2] = new SqlParameter("@SoLuong", obj.SoLuong);                
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@MaPhieuXuatHang", obj.MaPhieuXuatHang);
                par[5] = new SqlParameter("@MaPhieuHuyXuatHang", obj.MaPhieuHuyXuatHang);
                par[6] = new SqlParameter("@MaKho", obj.MaKho);
                par[7] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                par[8] = new SqlParameter("@NgayTao", obj.NgayNhap);

                if (!SqlHelper.IsHaveTran())
                    kq = SqlHelper.ExecuteNonQuery(spName, par);
                else
                {
                    kq = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                }


            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int InsertCHI_TIET_PXH(CHI_TIET_PXH obj)
        {
            string spName = "";
            SqlParameter[] par;
            int iResult = 0;

            try
            {
                SqlHelper.OpenConnection();

                spName = "usp_InsertCHI_TIET_PXH";
                par = new SqlParameter[12];
                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@SoLuong", obj.SoLuong);
                par[2] = new SqlParameter("@MaThietBi", obj.MaThietBi);
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@MaPhieuXuatHang", obj.MaPhieuXuatHang);
                par[5] = new SqlParameter("@MaPhieuHuyXuatHang", obj.MaPhieuHuyXuatHang);
                par[6] = new SqlParameter("@MaKho", obj.MaKho);
                par[7] = new SqlParameter("@MaTrangThai", obj.MaTrangThai);
                par[8] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                par[9] = new SqlParameter("@NgayTao", obj.NgayNhap);
                par[10] = new SqlParameter("@POThamChieuSAP", obj.POThamChieuSAP);
                par[11] = new SqlParameter("@MaChiTietPDH", obj.MaChiTietPDH);


                iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
            }
            catch 
            {

                iResult = -1;

            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return iResult;
        }
        #endregion
    }
}
